<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /> 
    <title>redis 系列，要懂redis，首先得看懂sds（全网最细节的sds讲解） - 天地维杰网</title>
    <meta name="keywords" content="系统架构,shutdown,不与天斗,Domino,博客,程序员,架构师,笔记,技术,分享,java,Redis">
    
    <meta property="og:title" content="redis 系列，要懂redis，首先得看懂sds（全网最细节的sds讲解）">
    <meta property="og:site_name" content="天地维杰网">
    <meta property="og:image" content="/img/author.jpg"> 
    <meta name="title" content="redis 系列，要懂redis，首先得看懂sds（全网最细节的sds讲解） - 天地维杰网" />
    <meta name="description" content="redis 系列，要懂redis，首先得看懂sds（全网最细节的sds讲解）本篇文章会讲到redis里面用到的最基础的数据类型sds，它相比较于普通string有什么不同了" />
     
    <link rel="shortcut icon" href="http://www.shutdown.cn/img/favicon.ico" />
    <link rel="apple-touch-icon" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link rel="apple-touch-icon-precomposed" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link href="http://www.shutdown.cn/js/vendor/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/main.css" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/syntax.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
     fancybox: true, 
    motion: true
  };
</script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-7826003325059020" crossorigin="anonymous"></script>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container one-collumn sidebar-position-left page-home  ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"> <div class="site-meta  custom-logo ">

  <div class="custom-logo-site-title">
    <a href="http://www.shutdown.cn"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">天地维杰网</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">人如秋鸿来有信，事若春梦了无痕</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
    <ul id="menu" class="menu">
      
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/redis/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-full"></i> <br />Redis
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/java/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-coffee"></i> <br />java
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/linux/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-linux"></i> <br />linux
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/daily/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bug"></i> <br />日常问题
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/spring/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-child"></i> <br />Spring和Springboot
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/mac/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-fire"></i> <br />Mac相关
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/middleware/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-gavel"></i> <br />中间件
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jiagou/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-rocket"></i> <br />架构
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/python/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-ship"></i> <br />python
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/front/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bolt"></i> <br />前端
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jvm/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-balance-scale"></i> <br />jvm
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/c/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-empty"></i> <br />c语言
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/web3/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-web3"></i> <br />web3
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/post/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/about/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于
          </a>
        </li>
      
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger"> <i class="menu-item-icon fa fa-search fa-fw"></i> <br /> 搜索</a>
      </li>
    </ul>
    <div class="site-search">
      <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>

    </div>
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
<section id="posts" class="posts-expand">
  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <header class="post-header">
      <h1 class="post-title" itemprop="name headline">
        <a class="post-title-link" href="http://www.shutdown.cn/post/understand-redis-sds/" itemprop="url">
        redis 系列，要懂redis，首先得看懂sds（全网最细节的sds讲解）
        </a>
      </h1>
      <div class="post-meta">
      <span class="post-time">
<span class="post-meta-item-icon">
    <i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">时间：</span>
<time itemprop="dateCreated" datetime="2016-03-22T13:04:35+08:00" content="2022-10-10">
    2022-10-10
</time>
</span> 
      

  <span class="post-category" >
  &nbsp; | &nbsp;
  <span class="post-meta-item-icon">
    <i class="fa fa-folder-o"></i>
  </span>
  <span class="post-meta-item-text">分类：</span>
  
    <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
      <a href="http://www.shutdown.cn/categories/redis" itemprop="url" rel="index">
        <span itemprop="name">redis</span>
      </a>
      &nbsp; 
    </span>
  
</span>


      
 <span>
&nbsp; | &nbsp;
<span class="post-meta-item-icon">
    <i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">转载</span>
</span>

       <span>
&nbsp; | &nbsp;
<span class="post-meta-item-icon">
    <i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读：</span>
<span class="leancloud-visitors-count">3885 字 ~8分钟</span>
</span>
      </div>
    </header>
    <div class="post-body" itemprop="articleBody">
    
    <div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback"> 文章系转载，便于整理和分类，原文地址：https://blog.csdn.net/qq_33361976/article/details/109014012</code></pre></div>
    

    

<h2 id="前言">前言</h2>

<p>本篇文章会讲到redis里面用到的最基础的数据类型sds，它相比较于普通string有什么不同了</p>

<hr />

<p>废话不多说，下面正式进入主题</p>

<h2 id="一-sds的结构">一、sds的结构</h2>

<h3 id="1-sds的定义">1. sds的定义</h3>

<p>sds.h</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-c" data-lang="c"><span style="color:#8f5902;font-style:italic">//定义了一个char 指针
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">typedef</span> <span style="color:#204a87;font-weight:bold">char</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">sds</span><span style="color:#000;font-weight:bold">;</span>

<span style="color:#8f5902;font-style:italic">/* Note: sdshdr5 is never used, we just access the flags byte directly.
</span><span style="color:#8f5902;font-style:italic"> * However is here to document the layout of type 5 SDS strings. */</span>

<span style="color:#204a87;font-weight:bold">struct</span> <span style="color:#000">__attribute__</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">__packed__</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000">sdshdr5</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#204a87;font-weight:bold">unsigned</span> <span style="color:#204a87;font-weight:bold">char</span> <span style="color:#000">flags</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">/* 3 lsb of type, and 5 msb of string length */</span>
    <span style="color:#204a87;font-weight:bold">char</span> <span style="color:#000">buf</span><span style="color:#000;font-weight:bold">[</span><span style="color:#000;font-weight:bold">]</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000;font-weight:bold">}</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#8f5902;font-style:italic">//lsb 代表有效位的意思，
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">//__attribute__ ((__packed__)) 代表structure 采用手动对齐的方式。
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">struct</span> <span style="color:#000">__attribute__</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">__packed__</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000">sdshdr8</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#8f5902;font-style:italic">//buf 已经使用的长度
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#000">uint8_t</span> <span style="color:#000">len</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">/* used */</span>
    <span style="color:#8f5902;font-style:italic">//buf 分配的长度，等于buf[]的总长度-1，因为buf有包括一个/0的结束符
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#000">uint8_t</span> <span style="color:#000">alloc</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">/* excluding the header and null terminator */</span>
    <span style="color:#8f5902;font-style:italic">//只有3位有效位，因为类型的表示就是0到4，所有这个8位的flags 有5位没有被用到
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#204a87;font-weight:bold">unsigned</span> <span style="color:#204a87;font-weight:bold">char</span> <span style="color:#000">flags</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">/* 3 lsb of type, 5 unused bits */</span>
    <span style="color:#8f5902;font-style:italic">//实际的字符串存在这里
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#204a87;font-weight:bold">char</span> <span style="color:#000">buf</span><span style="color:#000;font-weight:bold">[</span><span style="color:#000;font-weight:bold">]</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000;font-weight:bold">}</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#8f5902;font-style:italic">// 与上面的变化只有len和alloc， 就是长度不同而已
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">struct</span> <span style="color:#000">__attribute__</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">__packed__</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000">sdshdr16</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#000">uint16_t</span> <span style="color:#000">len</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">/* used */</span>
    <span style="color:#000">uint16_t</span> <span style="color:#000">alloc</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">/* excluding the header and null terminator */</span>
    <span style="color:#204a87;font-weight:bold">unsigned</span> <span style="color:#204a87;font-weight:bold">char</span> <span style="color:#000">flags</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">/* 3 lsb of type, 5 unused bits */</span>
    <span style="color:#204a87;font-weight:bold">char</span> <span style="color:#000">buf</span><span style="color:#000;font-weight:bold">[</span><span style="color:#000;font-weight:bold">]</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000;font-weight:bold">}</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#8f5902;font-style:italic">// 与上面的变化只有len和alloc， 就是长度不同而已
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">struct</span> <span style="color:#000">__attribute__</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">__packed__</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000">sdshdr32</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#000">uint32_t</span> <span style="color:#000">len</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">/* used */</span>
    <span style="color:#000">uint32_t</span> <span style="color:#000">alloc</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">/* excluding the header and null terminator */</span>
    <span style="color:#204a87;font-weight:bold">unsigned</span> <span style="color:#204a87;font-weight:bold">char</span> <span style="color:#000">flags</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">/* 3 lsb of type, 5 unused bits */</span>
    <span style="color:#204a87;font-weight:bold">char</span> <span style="color:#000">buf</span><span style="color:#000;font-weight:bold">[</span><span style="color:#000;font-weight:bold">]</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000;font-weight:bold">}</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#8f5902;font-style:italic">// 与上面的变化只有len和alloc， 就是长度不同而已
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">struct</span> <span style="color:#000">__attribute__</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">__packed__</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000">sdshdr64</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#000">uint64_t</span> <span style="color:#000">len</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">/* used */</span>
    <span style="color:#000">uint64_t</span> <span style="color:#000">alloc</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">/* excluding the header and null terminator */</span>
    <span style="color:#204a87;font-weight:bold">unsigned</span> <span style="color:#204a87;font-weight:bold">char</span> <span style="color:#000">flags</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">/* 3 lsb of type, 5 unused bits */</span>
    <span style="color:#204a87;font-weight:bold">char</span> <span style="color:#000">buf</span><span style="color:#000;font-weight:bold">[</span><span style="color:#000;font-weight:bold">]</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000;font-weight:bold">}</span><span style="color:#000;font-weight:bold">;</span></code></pre></div>
<p>可以看到sds 这种结构在不同长度下，大体的结构是相同，唯一不同的 sdshdr5也没用到过。</p>

<h3 id="2-sds的初始化">2. sds的初始化</h3>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-c" data-lang="c"><span style="color:#8f5902;font-style:italic">/* Create a new sds string with the content specified by the &#39;init&#39; pointer
</span><span style="color:#8f5902;font-style:italic"> * and &#39;initlen&#39;.
</span><span style="color:#8f5902;font-style:italic"> * If NULL is used for &#39;init&#39; the string is initialized with zero bytes.
</span><span style="color:#8f5902;font-style:italic"> * If SDS_NOINIT is used, the buffer is left uninitialized;
</span><span style="color:#8f5902;font-style:italic"> *
</span><span style="color:#8f5902;font-style:italic"> * The string is always null-termined (all the sds strings are, always) so
</span><span style="color:#8f5902;font-style:italic"> * even if you create an sds string with:
</span><span style="color:#8f5902;font-style:italic"> *
</span><span style="color:#8f5902;font-style:italic"> * mystring = sdsnewlen(&#34;abc&#34;,3);
</span><span style="color:#8f5902;font-style:italic"> *
</span><span style="color:#8f5902;font-style:italic"> * You can print the string with printf() as there is an implicit \0 at the
</span><span style="color:#8f5902;font-style:italic"> * end of the string. However the string is binary safe and can contain
</span><span style="color:#8f5902;font-style:italic"> * \0 characters in the middle, as the length is stored in the sds header. */</span>
<span style="color:#8f5902;font-style:italic">// 因为sds 在最前面指定了整个字符的长度，即使中间出现\0结束符，也能正常编译， 而不至于担心因为出现了结束符/0，而导致解码失败。
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">sds</span> <span style="color:#000">sdsnewlen</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">const</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">init</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">size_t</span> <span style="color:#000">initlen</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#8f5902;font-style:italic">// 这个指针会指向整个sds开始的地方
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">sh</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#8f5902;font-style:italic">// sds 实际上也是一个指针，
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#8f5902;font-style:italic">// 但是s指向整个struct buf开始的位置
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#000">sds</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#8f5902;font-style:italic">//根据不同的长度返回不同的类型的sds
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#204a87;font-weight:bold">char</span> <span style="color:#000">type</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">sdsReqType</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">initlen</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#8f5902;font-style:italic">/* Empty strings are usually created in order to append. Use type 8
</span><span style="color:#8f5902;font-style:italic">     * since type 5 is not good at this. */</span>
    <span style="color:#8f5902;font-style:italic">// 空string 往往用于append ， 在这里可以看到sds type 5 类型会被怒sds type 8 替换掉
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">type</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">SDS_TYPE_5</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">initlen</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000">type</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">SDS_TYPE_8</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#8f5902;font-style:italic">//获取struct的长度
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">hdrlen</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">sdsHdrSize</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">type</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#8f5902;font-style:italic">// flag 指针，这个指针就是用来表示sds 是哪个类型的
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#204a87;font-weight:bold">unsigned</span> <span style="color:#204a87;font-weight:bold">char</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">fp</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">/* flags pointer. */</span>
    <span style="color:#8f5902;font-style:italic">//分配空间 这里+1 为的是分配一个结束符号
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#000">sh</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">s_malloc</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">hdrlen</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000">initlen</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#8f5902;font-style:italic">// sh在这里指向了这个刚刚分配的内存地址
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">sh</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87">NULL</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#204a87">NULL</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#8f5902;font-style:italic">// 判断是否是init阶段
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">init</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#000">SDS_NOINIT</span><span style="color:#000;font-weight:bold">)</span>
        <span style="color:#000">init</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87">NULL</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#8f5902;font-style:italic">// 如果不是init阶段则清0
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#204a87;font-weight:bold">else</span> <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">!</span><span style="color:#000">init</span><span style="color:#000;font-weight:bold">)</span>
        <span style="color:#8f5902;font-style:italic">//init 不为空的话
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#8f5902;font-style:italic">// 将sh这块内存全部设置为0
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#000">memset</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">sh</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">hdrlen</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000">initlen</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#8f5902;font-style:italic">//s 指向了字符串开始的地址，hdrlen 可以看错sds head
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#000">s</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000">sh</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000">hdrlen</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#8f5902;font-style:italic">//因为可以看到地址的顺序是 len,alloc,flag,buf,目前s是指向buf，
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#8f5902;font-style:italic">//那么后退1位,fp 正好指向了flag对应的地址。
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#000">fp</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">unsigned</span> <span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#8f5902;font-style:italic">//下面就是根据
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#204a87;font-weight:bold">switch</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">type</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_5</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">fp</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">type</span> <span style="color:#ce5c00;font-weight:bold">|</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">initlen</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#000">SDS_TYPE_BITS</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">break</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000;font-weight:bold">}</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_8</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#8f5902;font-style:italic">//这里使用了内连方法，让sh这个变量赋值了struct sdshdr
</span><span style="color:#8f5902;font-style:italic"></span>            <span style="color:#000">SDS_HDR_VAR</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">8</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#8f5902;font-style:italic">//下面就是初始化长度，这里就没啥好说的了
</span><span style="color:#8f5902;font-style:italic"></span>            <span style="color:#000">sh</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">len</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">initlen</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000">sh</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">alloc</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">initlen</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#8f5902;font-style:italic">//fp 对应的地址赋值了对应的type，type 的取值是1-4
</span><span style="color:#8f5902;font-style:italic"></span>            <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">fp</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">type</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">break</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000;font-weight:bold">}</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_16</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#000">SDS_HDR_VAR</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">16</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000">sh</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">len</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">initlen</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000">sh</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">alloc</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">initlen</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">fp</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">type</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">break</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000;font-weight:bold">}</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_32</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#000">SDS_HDR_VAR</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">32</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000">sh</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">len</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">initlen</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000">sh</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">alloc</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">initlen</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">fp</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">type</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">break</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000;font-weight:bold">}</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_64</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#000">SDS_HDR_VAR</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">64</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000">sh</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">len</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">initlen</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000">sh</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">alloc</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">initlen</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">fp</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">type</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">break</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000;font-weight:bold">}</span>
    <span style="color:#000;font-weight:bold">}</span>
    <span style="color:#8f5902;font-style:italic">//如果两者都不为空，则init 这个对应的字符串，赋值给s
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">initlen</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">init</span><span style="color:#000;font-weight:bold">)</span>
        <span style="color:#8f5902;font-style:italic">//这里是给buf赋值初始化
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#000">memcpy</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">init</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">initlen</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#8f5902;font-style:italic">// 分配一个结束符   
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#000">s</span><span style="color:#000;font-weight:bold">[</span><span style="color:#000">initlen</span><span style="color:#000;font-weight:bold">]</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#4e9a06"></span><span style="color:#4e9a06">&#39;</span><span style="color:#4e9a06">\0</span><span style="color:#4e9a06">&#39;</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000;font-weight:bold">}</span>

<span style="color:#8f5902;font-style:italic">// 下面的方法，为上面的init 里面用到的工具方法
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">// 根据不同的string size 返回不同sds类型
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">inline</span> <span style="color:#204a87;font-weight:bold">char</span> <span style="color:#000">sdsReqType</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">size_t</span> <span style="color:#000">string_size</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">string_size</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#0000cf;font-weight:bold">1</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#0000cf;font-weight:bold">5</span><span style="color:#000;font-weight:bold">)</span>
        <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">SDS_TYPE_5</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">string_size</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#0000cf;font-weight:bold">1</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#0000cf;font-weight:bold">8</span><span style="color:#000;font-weight:bold">)</span>
        <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">SDS_TYPE_8</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">string_size</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#0000cf;font-weight:bold">1</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#0000cf;font-weight:bold">16</span><span style="color:#000;font-weight:bold">)</span>
        <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">SDS_TYPE_16</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#8f5902;font-style:italic">//这里应该是考虑到32位系统的原因   
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">if (LONG_MAX == LLONG_MAX)</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">string_size</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#0000cf;font-weight:bold">1ll</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#0000cf;font-weight:bold">32</span><span style="color:#000;font-weight:bold">)</span>
        <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">SDS_TYPE_32</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">SDS_TYPE_64</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">else</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">SDS_TYPE_32</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">endif</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000;font-weight:bold">}</span>
<span style="color:#8f5902;font-style:italic">// 根据不同的类型返回struct的长度
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">inline</span> <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">sdsHdrSize</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">char</span> <span style="color:#000">type</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#8f5902;font-style:italic">//SDS_TYPE_MASK的作用是清除不必要的位数 SDS_TYPE_MASK 为00000111
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#204a87;font-weight:bold">switch</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">type</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">SDS_TYPE_MASK</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_5</span><span style="color:#000;font-weight:bold">:</span>
            <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">struct</span> <span style="color:#000">sdshdr5</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_8</span><span style="color:#000;font-weight:bold">:</span>
            <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">struct</span> <span style="color:#000">sdshdr8</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_16</span><span style="color:#000;font-weight:bold">:</span>
            <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">struct</span> <span style="color:#000">sdshdr16</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_32</span><span style="color:#000;font-weight:bold">:</span>
            <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">struct</span> <span style="color:#000">sdshdr32</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_64</span><span style="color:#000;font-weight:bold">:</span>
            <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">struct</span> <span style="color:#000">sdshdr64</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span>
    <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000;font-weight:bold">}</span>
<span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">define SDS_TYPE_5  0</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">define SDS_TYPE_8  1</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">define SDS_TYPE_16 2</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">define SDS_TYPE_32 3</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">define SDS_TYPE_64 4</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">define SDS_TYPE_MASK 7</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">define SDS_TYPE_BITS 3</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">// 可以看到s本来是指向buf的位置，减去struct的长度正好就是开始的位置
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">// 然后sh就指向了整个sds开始的位置
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">// T就是占位符，返回不同类型的struct
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">define SDS_HDR_VAR(T,s) struct sdshdr##T *sh = (void*)((s)-(sizeof(struct sdshdr##T)));</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">//下面这个就是直接返回对应的struct
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">define SDS_HDR(T,s) ((struct sdshdr##T *)((s)-(sizeof(struct sdshdr##T))))</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">//这个类型的可以不关注因为没有被用到
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">define SDS_TYPE_5_LEN(f) ((f)&gt;&gt;SDS_TYPE_BITS)</span></code></pre></div>
<h3 id="3-sds-图解">3. sds 图解</h3>

<div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/20221010193419.png" width = 900 /> </div>

<h3 id="4-sds-扩容">4. sds 扩容</h3>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-c" data-lang="c"><span style="color:#8f5902;font-style:italic">/* Enlarge the free space at the end of the sds string so that the caller
</span><span style="color:#8f5902;font-style:italic"> * is sure that after calling this function can overwrite up to addlen
</span><span style="color:#8f5902;font-style:italic"> * bytes after the end of the string, plus one more byte for nul term.
</span><span style="color:#8f5902;font-style:italic"> *
</span><span style="color:#8f5902;font-style:italic"> * Note: this does not change the *length* of the sds string as returned
</span><span style="color:#8f5902;font-style:italic"> * by sdslen(), but only the free buffer space we have. */</span>
<span style="color:#8f5902;font-style:italic">// 扩容sds， 这里有一点，如果sds本身还有剩余空间，那么多分配的空间等于 addlen-leftlen
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">sds</span> <span style="color:#000">sdsMakeRoomFor</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">sds</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">size_t</span> <span style="color:#000">addlen</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">sh</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">newsh</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#8f5902;font-style:italic">//获取剩余可用的空间
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#000">size_t</span> <span style="color:#000">avail</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">sdsavail</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000">size_t</span> <span style="color:#000">len</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">newlen</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#8f5902;font-style:italic">//在上面图解里面s的指针是这段空间的中间 ,那么-1就正好指向了flag
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#204a87;font-weight:bold">char</span> <span style="color:#000">type</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">oldtype</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">[</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">]</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">SDS_TYPE_MASK</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">hdrlen</span><span style="color:#000;font-weight:bold">;</span>

    <span style="color:#8f5902;font-style:italic">/* Return ASAP if there is enough space left. */</span>
    <span style="color:#8f5902;font-style:italic">//如果可用空间大于需要增加的长度，那么直接返回
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">avail</span> <span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">addlen</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#8f5902;font-style:italic">//len 已使用长度
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#000">len</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">sdslen</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#8f5902;font-style:italic">//sh 回到指向了这个sds的起始位置。
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#000">sh</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000">s</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">sdsHdrSize</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">oldtype</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#8f5902;font-style:italic">// newlen 代表最小需要的长度
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#000">newlen</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">len</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000">addlen</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">newlen</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#000">SDS_MAX_PREALLOC</span><span style="color:#000;font-weight:bold">)</span>
        <span style="color:#8f5902;font-style:italic">//但是实际会分配2倍的长度，如果空间小于最大阈值
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#000">newlen</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">2</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">else</span>
        <span style="color:#000">newlen</span> <span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">SDS_MAX_PREALLOC</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#8f5902;font-style:italic">//获取新长度的类型
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#000">type</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">sdsReqType</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">newlen</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>

    <span style="color:#8f5902;font-style:italic">/* Don&#39;t use type 5: the user is appending to the string and type 5 is
</span><span style="color:#8f5902;font-style:italic">     * not able to remember empty space, so sdsMakeRoomFor() must be called
</span><span style="color:#8f5902;font-style:italic">     * at every appending operation. */</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">type</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">SDS_TYPE_5</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000">type</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">SDS_TYPE_8</span><span style="color:#000;font-weight:bold">;</span>

    <span style="color:#000">hdrlen</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">sdsHdrSize</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">type</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">oldtype</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#000">type</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#8f5902;font-style:italic">//sh是开始地址，在开始地址的基础上，分配更多的空间，
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#8f5902;font-style:italic">// 逻辑如同初始化部分，hdrlen 是head的长度，即struct本身大小
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#8f5902;font-style:italic">// 后面newlen 是buf 大小， +1 是为了结束符号
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#8f5902;font-style:italic">// sds 通常情况下是可以直接打印的
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#000">newsh</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">s_realloc</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">sh</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">hdrlen</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000">newlen</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">newsh</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87">NULL</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#204a87">NULL</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#8f5902;font-style:italic">//s继续指向中间位置
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#000">s</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000">newsh</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000">hdrlen</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span> <span style="color:#204a87;font-weight:bold">else</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#8f5902;font-style:italic">/* Since the header size changes, need to move the string forward,
</span><span style="color:#8f5902;font-style:italic">         * and can&#39;t use realloc */</span>
        <span style="color:#8f5902;font-style:italic">//如果类型发生变化，地址内容不可复用，所以找新的空间。
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#000">newsh</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">s_malloc</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">hdrlen</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000">newlen</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">newsh</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87">NULL</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#204a87">NULL</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#8f5902;font-style:italic">//复制原来的str到新的sds 上面,
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#8f5902;font-style:italic">//newsh+hdrlen 等于sds buf 地址开始的位置
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#8f5902;font-style:italic">//s 原buf的位置
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#8f5902;font-style:italic">//len+1 把结束符号也复制进来
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#000">memcpy</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000">newsh</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000">hdrlen</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">len</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#8f5902;font-style:italic">//释放前面的内存空间
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#000">s_free</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">sh</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#8f5902;font-style:italic">//调整s开始的位置，即地址空间指向新的buf开始的位置
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#000">s</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000">newsh</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000">hdrlen</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#8f5902;font-style:italic">//-1 正好到了flag的位置
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#000">s</span><span style="color:#000;font-weight:bold">[</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">]</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">type</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#8f5902;font-style:italic">//分配len的值
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#000">sdssetlen</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">len</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span>
    <span style="color:#8f5902;font-style:italic">//分配alloc的值
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#000">sdssetalloc</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">newlen</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#8f5902;font-style:italic">//返回新的sds
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000;font-weight:bold">}</span>
<span style="color:#8f5902;font-style:italic">// 给len 设值
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">inline</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">sdssetlen</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">sds</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">size_t</span> <span style="color:#000">newlen</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#204a87;font-weight:bold">unsigned</span> <span style="color:#204a87;font-weight:bold">char</span> <span style="color:#000">flags</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">[</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">]</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">switch</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">flags</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">SDS_TYPE_MASK</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_5</span><span style="color:#000;font-weight:bold">:</span>
            <span style="color:#000;font-weight:bold">{</span>
                <span style="color:#204a87;font-weight:bold">unsigned</span> <span style="color:#204a87;font-weight:bold">char</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">fp</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">unsigned</span> <span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">;</span>
                <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">fp</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">SDS_TYPE_5</span> <span style="color:#ce5c00;font-weight:bold">|</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">newlen</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#000">SDS_TYPE_BITS</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000;font-weight:bold">}</span>
            <span style="color:#204a87;font-weight:bold">break</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_8</span><span style="color:#000;font-weight:bold">:</span>
            <span style="color:#000">SDS_HDR</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">8</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">len</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">newlen</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">break</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_16</span><span style="color:#000;font-weight:bold">:</span>
            <span style="color:#000">SDS_HDR</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">16</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">len</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">newlen</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">break</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_32</span><span style="color:#000;font-weight:bold">:</span>
            <span style="color:#000">SDS_HDR</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">32</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">len</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">newlen</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">break</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_64</span><span style="color:#000;font-weight:bold">:</span>
            <span style="color:#000">SDS_HDR</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">64</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">len</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">newlen</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">break</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span>
<span style="color:#000;font-weight:bold">}</span>
<span style="color:#8f5902;font-style:italic">// 获取当前sds,可用的长度。
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">inline</span> <span style="color:#000">size_t</span> <span style="color:#000">sdsavail</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">const</span> <span style="color:#000">sds</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#204a87;font-weight:bold">unsigned</span> <span style="color:#204a87;font-weight:bold">char</span> <span style="color:#000">flags</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">[</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">]</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">switch</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">flags</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">SDS_TYPE_MASK</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_5</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000;font-weight:bold">}</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_8</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#000">SDS_HDR_VAR</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">8</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">sh</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">alloc</span> <span style="color:#ce5c00;font-weight:bold">-</span> <span style="color:#000">sh</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">len</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000;font-weight:bold">}</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_16</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#000">SDS_HDR_VAR</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">16</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">sh</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">alloc</span> <span style="color:#ce5c00;font-weight:bold">-</span> <span style="color:#000">sh</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">len</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000;font-weight:bold">}</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_32</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#000">SDS_HDR_VAR</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">32</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">sh</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">alloc</span> <span style="color:#ce5c00;font-weight:bold">-</span> <span style="color:#000">sh</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">len</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000;font-weight:bold">}</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_64</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#000">SDS_HDR_VAR</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">64</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">sh</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">alloc</span> <span style="color:#ce5c00;font-weight:bold">-</span> <span style="color:#000">sh</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">len</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000;font-weight:bold">}</span>
    <span style="color:#000;font-weight:bold">}</span>
    <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000;font-weight:bold">}</span>

<span style="color:#8f5902;font-style:italic">/* sdsalloc() = sdsavail() + sdslen() */</span>
<span style="color:#8f5902;font-style:italic">// 获取alloc的长度
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">inline</span> <span style="color:#000">size_t</span> <span style="color:#000">sdsalloc</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">const</span> <span style="color:#000">sds</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#204a87;font-weight:bold">unsigned</span> <span style="color:#204a87;font-weight:bold">char</span> <span style="color:#000">flags</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">[</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">]</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">switch</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">flags</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">SDS_TYPE_MASK</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_5</span><span style="color:#000;font-weight:bold">:</span>
            <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">SDS_TYPE_5_LEN</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">flags</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_8</span><span style="color:#000;font-weight:bold">:</span>
            <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">SDS_HDR</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">8</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">alloc</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_16</span><span style="color:#000;font-weight:bold">:</span>
            <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">SDS_HDR</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">16</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">alloc</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_32</span><span style="color:#000;font-weight:bold">:</span>
            <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">SDS_HDR</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">32</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">alloc</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_64</span><span style="color:#000;font-weight:bold">:</span>
            <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">SDS_HDR</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">64</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">alloc</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span>
    <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000;font-weight:bold">}</span>
<span style="color:#8f5902;font-style:italic">// 给 alloc 设值
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">inline</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">sdssetalloc</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">sds</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">size_t</span> <span style="color:#000">newlen</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#204a87;font-weight:bold">unsigned</span> <span style="color:#204a87;font-weight:bold">char</span> <span style="color:#000">flags</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">[</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">]</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">switch</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">flags</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">SDS_TYPE_MASK</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_5</span><span style="color:#000;font-weight:bold">:</span>
            <span style="color:#8f5902;font-style:italic">/* Nothing to do, this type has no total allocation info. */</span>
            <span style="color:#204a87;font-weight:bold">break</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_8</span><span style="color:#000;font-weight:bold">:</span>
            <span style="color:#000">SDS_HDR</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">8</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">alloc</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">newlen</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">break</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_16</span><span style="color:#000;font-weight:bold">:</span>
            <span style="color:#000">SDS_HDR</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">16</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">alloc</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">newlen</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">break</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_32</span><span style="color:#000;font-weight:bold">:</span>
            <span style="color:#000">SDS_HDR</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">32</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">alloc</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">newlen</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">break</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#f57900">SDS_TYPE_64</span><span style="color:#000;font-weight:bold">:</span>
            <span style="color:#000">SDS_HDR</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">64</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">alloc</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">newlen</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">break</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span>
<span style="color:#000;font-weight:bold">}</span></code></pre></div>
<p><strong>上面主要讲了sds是如何扩容的，可以看到sds的最大特点是可以预分配内存，在扩容方面也非常的高效。不用复制来复制去</strong></p>

<h3 id="5-为什么要使用内存不对齐-非常重要-全网讲sds唯一讲到这点就在这里">5. 为什么要使用内存不对齐（非常重要，全网讲sds唯一讲到这点就在这里）</h3>

<p>在看下面之前首先要对内存对齐有个基本概念，为什么要内存对齐了，这里就设计到cpu的工作原理相关了，可以百度cpu工作原理，主要是跟内存地址和寄存器的映射关系有关，但是有两条定理可以在这里了解。</p>

<ol>
<li><p>一个32位的cpu， 每一个周期从内存里面能读到32位的数据。</p></li>

<li><p>基于这个原因和寄存器的原因，cpu的每次读的地址开始是4的倍数，打个比方我们要读地址2上面长度为4的数据，那么就需要两个周期， cpu首先得从0-3地址上面读数据，然后再从3-7的地址上面的数据， 在这里我们可以看到内存对齐的作用。
那么问题来了，对于redis 作者一个对内存和cpu 用到极致的作者，为什么还要用非对齐的sds了，原因在于sds的本身结构注定只能非对齐状态。
请看下图，在对齐状态我们的结构体在内存里面表现形势是如何的。</p></li>
</ol>

<p><div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/20221010193458.png" width = 900 /> </div></p>

<p>可以看到不同类型的sds 下面，pad 的位数也是不同的，那么我们要从sds 指针位置访问到flag，在不知道类型的情况下是不可能了，那么有同学又要发问了，去掉sdshdr8的结构不就行了吗，从理论来说这样牺牲的内存也不会太多，也保证了性能，但是这仅仅是在32位系统下面的结构，如果在64位系统，那可能又是另外一个结构了。 好的那么有同学又要说了 我们能不能把指针放到flag开始的位置。答案也是不能，但是：</p>

<ol>
<li><p>这样我们就没办法完美兼容string，</p></li>

<li><p>这样我们也会引入各种类型判断调整，所以redis 最后还是用到内存不对齐这个方案。</p></li>
</ol>

<h2 id="总结">总结</h2>

<ol>
<li>可以看到sds相较于普通c语言string 来说，是二进制安全的，因为string 没有长度标示当一段地址里面存在多个结束符号，string是没有办法访问到第一个/0后面的内容的，而正好sds的结构弥补了这一缺点。</li>
<li>sds 对于动态扩容也是string 无法比拟的，如sds的结构让sds可以预分配内存，甚至在原有长度类型不变的基础上，可以在原来内存使用基础上。实现动态扩容，上文代码也讲解得非常清晰。那么这样的好处又是啥了，如果一个string 非常长的情况下，如果要在原来的String基础上做append 操作，那么需要把内容复制到新的地址上面，而这件事情是一件非常耗费性能的一件事，而sds正好解决这件事，尤其在将网络io数据转化为具体命令操作的时候，要经常对字符串做append操作。所以sds结构非常适合redis。</li>
<li>且sds也可以直接当做string来使用，<strong>巧妙的指针使用</strong>也是让sds完美兼容string。
至于为什么要讲sds，因为在redis 最基础的数据除了字典就是sds，所以再我们讲redis如何从网络报文转化为具体执行的命令前，我们得先熟悉下sds这个结构。</li>
</ol>

    </div>
    <footer class="post-footer">
     
 
<div class="post-tags">     
     
    <a href="http://www.shutdown.cn/tags/redis" rel="tag" title="redis">#redis#</a>
    
    <a href="http://www.shutdown.cn/tags/sds" rel="tag" title="sds">#sds#</a>
    
    <a href="http://www.shutdown.cn/tags/string" rel="tag" title="string">#string#</a>
    
</div>



     <div class="post-nav">
    <div class="post-nav-next post-nav-item">
    
        <a href="http://www.shutdown.cn/post/hugo-problem-issuses/" rel="next" title="hugo 博客的一些问题记录">
        <i class="fa fa-chevron-left"></i> hugo 博客的一些问题记录
        </a>
    
    </div>

    <div class="post-nav-prev post-nav-item">
    
        <a href="http://www.shutdown.cn/post/redis-rdb-aof-data-restore/" rel="prev" title="Redis Rdb Aof恢复">
        Redis Rdb Aof恢复 <i class="fa fa-chevron-right"></i>
        </a>
    
    </div>
</div>
      
     
     
     






    </footer>
  </article>
</section>

          </div>
        </div>
        <div class="sidebar-toggle">
  <div class="sidebar-toggle-line-wrap">
    <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
  </div>
</div>
<aside id="sidebar" class="sidebar">
  <div class="sidebar-inner">

  <ul class="sidebar-nav motion-element">
    <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
      文章目录
    </li>
    <li class="sidebar-nav-overview" data-target="site-overview">
      站点概览
    </li>
  </ul>

    <section class="site-overview sidebar-panel ">
      <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
        src="http://www.shutdown.cn/img/author.jpg"
        alt="不与天斗Domino" />
    <p class="site-author-name" itemprop="name">不与天斗Domino</p>
    <p class="site-description motion-element" itemprop="description"> 
        Programmer &amp; Architect</p>
</div>
      <nav class="site-state motion-element">
    <div class="site-state-item site-state-posts">
      <a href="http://www.shutdown.cn/post/">
        <span class="site-state-item-count">183</span>
        <span class="site-state-item-name">日志</span>
      </a>
    </div>
    <div class="site-state-item site-state-categories">    
        <a href="http://www.shutdown.cn/categories/">      
         
        <span class="site-state-item-count">15</span>
        
        <span class="site-state-item-name">分类</span>
        
        </a>
    </div>

    <div class="site-state-item site-state-tags">
        <a href="http://www.shutdown.cn/tags/">
         
        <span class="site-state-item-count">224</span>
        
        <span class="site-state-item-name">标签</span>
        </a>
    </div>
</nav>
      
      

      

      <div class="links-of-blogroll motion-element inline">
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?i=&amp;m=0&amp;s=220&amp;c=ff0000&amp;cr1=ffffff&amp;f=arial&amp;l=33&amp;bv=35" async="async"></script>
</div>

    </section>
    
<section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
    <div class="post-toc">
        <div class="post-toc-content"><nav id="TableOfContents">
<ul>
<li>
<ul>
<li><a href="#前言">前言</a></li>
<li><a href="#一-sds的结构">一、sds的结构</a>
<ul>
<li><a href="#1-sds的定义">1. sds的定义</a></li>
<li><a href="#2-sds的初始化">2. sds的初始化</a></li>
<li><a href="#3-sds-图解">3. sds 图解</a></li>
<li><a href="#4-sds-扩容">4. sds 扩容</a></li>
<li><a href="#5-为什么要使用内存不对齐-非常重要-全网讲sds唯一讲到这点就在这里">5. 为什么要使用内存不对齐（非常重要，全网讲sds唯一讲到这点就在这里）</a></li>
</ul></li>
<li><a href="#总结">总结</a></li>
</ul></li>
</ul>
</nav></div>
    </div>
</section>

  </div>
</aside>

      </div>
    </main>
   
    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  <span itemprop="copyrightYear">  &copy; 
  2013 - 2023</span>
  <span class="with-love"><i class="fa fa-heart"></i></span>
  <span class="author" itemprop="copyrightHolder">天地维杰网</span>
  <span class="icp" itemprop="copyrightHolder"><a href="https://beian.miit.gov.cn/" target="_blank">京ICP备13019191号-1</a></span>
</div>
<div class="powered-by">
  Powered by - <a class="theme-link" href="http://gohugo.io" target="_blank" title="hugo" >Hugo v0.63.2</a>
</div>
<div class="theme-info">
  Theme by - <a class="theme-link" href="https://github.com/xtfly/hugo-theme-next" target="_blank"> NexT
  </a>
</div>


      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
      <span id="scrollpercent"><span>0</span>%</span>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery/index.js?v=2.1.3"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/fastclick/lib/fastclick.min.js?v=1.0.6"></script> 
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.min.js?v=1.2.1"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="http://www.shutdown.cn/js/vendor/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>

<script src="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.pack.js?v=2.1.5"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/utils.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/motion.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/affix.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/schemes/pisces.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/scrollspy.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/post-details.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/toc.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/bootstrap.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js"],
    jax: ["input/TeX", "output/HTML-CSS"],
    tex2jax: {
      inlineMath: [ ['$','$'] ],
      displayMath: [ ['$$','$$'] ],
      processEscapes: true
    },
    "HTML-CSS": { fonts: ["TeX"] }
  });
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML' async></script>
</body>
</html>